<?php

namespace Biz\model\bigData;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class StoreWikiInfoDailyReport extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_bigdata_online';

    const _CONFIG_SELECT_ = 'bigdata_centre';

    const _DATABASE_ = 'hhz_bigdata_online';

    const _TABLE_NAME = 'store_wiki_info_daily_report';

    use BizModelTrait;

    /**
     * 获取7天数据
     * @param $storeId
     * @param $targetDate
     * @param $periodDays
     * @return array
     */
    public static function getPeriodData($storeId, $targetDate, $periodDays = 7)
    {
        $returnData = [
            'wiki_num' => 0,
            'exp_num' => 0,
            'pv' => 0,
            'favorite_num' => 0,
            'share_num' => 0,
        ];

        $where['store_id'] = $storeId;

        $where['day'] = self::getPeriodDays($targetDate, $periodDays);

        $list = self::dataSelect($where);

        if ($list && is_array($list)) {
            foreach ($list as $row) {
                $returnData['wiki_num'] += $row['wiki_num'];
                $returnData['exp_num'] += $row['exp_num'];
                $returnData['pv'] += $row['pv'];
                $returnData['favorite_num'] += $row['favorite_num'];
                $returnData['share_num'] += $row['share_num'];
            }
        }

        return $returnData;

    }


    /**
     * 获取统计周期
     * @param $targetDate
     * @param $periodDays
     * @return array
     */
    private static function getPeriodDays($targetDate, $periodDays)
    {
        $days = [];

        for ($i = -1; $i > 0 - $periodDays - 1; $i--) {
            $days[] = date('Ymd', strtotime("$i days", strtotime($targetDate)));
        }

        return $days;
    }
}
